;----------------------------
;	CONFIGURE OSC
;----------------------------

								;FOSC=FIN/N1*M/N2
								;CHECK PROGRAMMER MANUAL CHARPTER 42 FOR DETAIL
.EQU	OSCTUN_VAL,	5			;   CENTER FREQ = 7.5 MHz

.EQU	CLKDIV_VAL, 0X0001		;	N1=2, N2=3
.EQU	PLLFBD_VAL, 62
;.EQU	PLLFBD_VAL, 64			;	M=64 (PLL FEEDNACK DIVISOR=64)
.EQU	ACLKCON_VAL, 0XA740		;	ACLK=7.5MHz
.EQU	REFOCON_VAL, 0X8300		;	REFCLK=FOSC/8=10M



.INCLUDE	"p33FJ16GS502.INC"
.INCLUDE 	"MACRO.INC"

.GLOBAL		OSC_INITIAL				; DECLEAR GLOBAL SUBROUTINE: OSC INITIAL
.GLOBAL		ACLK_INITIAL			; DECLEAR GLOBAL SUBROUTINE: ACLK INITIAL
.GLOBAL		REFCLKO_INITIAL			; DECLEAR GLOBAL SUBROUTINE: REFCLKO INITIAL

.TEXT								; PROGRAM STARTING POINT		

OSC_INITIAL:
	MOVFF	#OSCTUN_VAL,OSCTUN		; "MOVFF" INSTRUCTION HAS BEEN INCLUDE IN HEADER FILES, NAMED "MACRO.INC"
	MOVFF	#PLLFBD_VAL,PLLFBD
	MOVFF	#CLKDIV_VAL,CLKDIV
	RETURN

PLL_NOT_LOCK:
	BTSS	OSCCON,#LOCK
	GOTO	PLL_NOT_LOCK
	RETURN

ACLK_INITIAL:
	MOVFF	#ACLKCON_VAL,ACLKCON
ACLK_NOT_LOCK:
	BTSS	ACLKCON,#APLLCK
	GOTO	ACLK_NOT_LOCK
	RETURN
REFCLKO_INITIAL:
	MOVFF	#REFOCON_VAL,REFOCON
	RETURN
		

	.END